import { _decorator, Asset, assetManager, Prefab } from "cc";
const { ccclass, property } = _decorator;

@ccclass('AssetsManager')
export class AssetsManager {
    private static _instance: AssetsManager

    static get instance(): AssetsManager {
        if (!this._instance) {
            this._instance = new AssetsManager()
        }

        return this._instance
    }

    private _bundle: Map<string, Asset> = new Map()
    
    loadBundle(name: string): Promise<any> {
        return new Promise((resolve, reject) => {
            assetManager.loadBundle(name, (_err, bundle) => {
                if (_err) reject(_err)

                bundle.loadDir('', Prefab, (err, assets) => {
                    if (err) reject(err)
                    assets.forEach(asset => {
                        this._bundle.set(asset.name, asset)
                    })
                    resolve('finish')
                })
            })
        })
    }

    getAsset(name: string): Asset {
        return this._bundle.get(name)
    }
}